#include "my_system.h"


_system_data system_data={

	.backlight = 10,
};

static void system_time_task(void *pvParameters)
{

#if USE_WIN == 0
	static int time=60*10-10;
    static TickType_t tick;

    tick = xTaskGetTickCount();
	esp_task_wdt_add(NULL);
    while(1)
    {
		esp_task_wdt_reset();

		system_data.RiQi_Data.miao += 1;
		if (system_data.RiQi_Data.miao >= 60)
		{
			system_data.RiQi_Data.miao = 0;

			system_data.RiQi_Data.fen += 1;
			if (system_data.RiQi_Data.fen >= 60)
			{
				system_data.RiQi_Data.fen = 0;
				system_data.RiQi_Data.shi += 1;
				if (system_data.RiQi_Data.shi >= 24)
					system_data.RiQi_Data.shi = 0;
			}
		}

		if(system_data.RiQi_Data.shi==24)
		{
			system_data.RiQi_Data.shi = 0;
		}else
		if(system_data.RiQi_Data.shi == 0)
		{
			lvgl_clock_data.Shi = 12;

		}else
		if(system_data.RiQi_Data.shi>12)
		{
			lvgl_clock_data.Shi -= 12;//system_data.RiQi_Data.shi-12;

		}

		lvgl_clock_style2_data.sec = system_data.RiQi_Data.miao;
		lvgl_clock_style2_data.min = system_data.RiQi_Data.fen;
		lvgl_clock_style2_data.hour = system_data.RiQi_Data.shi;

		lvgl_clock_data.Miao = system_data.RiQi_Data.miao*10;
		lvgl_clock_data.Fen = system_data.RiQi_Data.fen;
		lvgl_clock_data.Shi = system_data.RiQi_Data.shi;
		lvgl_clock_data.Nian = system_data.RiQi_Data.nian;
		lvgl_clock_data.Yue  = system_data.RiQi_Data.yue;
		lvgl_clock_data.Ri   = system_data.RiQi_Data.ri;


		if(++time>60*10*1)
		{
			time=0;
			if(system_data.WIFI_Sta == wifi_qingqiuduankai || system_data.WIFI_Sta == wifi_lianjiehzong)
			{

			}else
			{
				system_data.HuoQu_ShiJian_Flag=true;//同步一次时间
				system_give_https_request_Semaphore();//释放信号量
			}
		}
		vTaskDelayUntil(&tick, pdMS_TO_TICKS(1000));
    }
	vTaskDelete(NULL);
	esp_task_wdt_delete(NULL);
#endif
}

void system_give_https_request_Semaphore(void)
{
#if USE_WIN == 0
	xSemaphoreGive(system_data.https_request_Semaphore);
#endif
}
int system_init(void)
{
	system_data.Language = JianTiZhongWen;
	system_data.https_request_Semaphore = xSemaphoreCreateCounting(10,0);
	sprintf(system_data.wifi_name,"Xiaomi_GSM");
	sprintf(system_data.wifi_password,"17625349864");

	system_data.RiQi_Data.nian = 2021;
	system_data.RiQi_Data.yue = 1;
	system_data.RiQi_Data.ri = 14;
	system_data.RiQi_Data.shi = 0;
	system_data.RiQi_Data.fen = 0;
	system_data.RiQi_Data.miao = 0;

	sprintf(system_data.bilibili_Data.id,"%s",HTTPS_BILIBILI_ID);
	system_set_wifi_kaiguan(true);

	system_wifi_data = wifi_list_create("NULL","NULL","NULL");
	if(system_wifi_data)
	{
		system_debug("wifi链表创建成功");
	}

	system_read_config();

	//system_set_radio_onoff(1);
	system_set_radio_vol(2);
	system_set_radio_sig(8);
	system_set_radio_frequency(8910);
	system_set_radio_onoff(0);
	system_audio_select(AUDIO_SELECT_RADIO);
	system_set_spk_switch(SPK_SWITCH_OFF);


#if USE_WIN == 1

	system_set_radio_p(0,"88.8");
	system_set_radio_p(1,"89.1");
	system_set_radio_p(2,"95.7");
	system_set_radio_p(3,"98.2");
	system_set_radio_p(4,"102.8");

#endif

	#if USE_WIN == 0
		xTaskCreate(system_time_task, "system_time_task", 1024*2, NULL, 2, NULL);
	#endif

    return 0;
}

int system_set_backlight(int dat)
{
	system_data.backlight = dat;

#if USE_WIN == 0
	bsp_ledc_set_duty(system_data.backlight);

#endif
	return system_data.backlight;
}

int system_get_backlight(void)
{
	return system_data.backlight;
}

void system_set_radio_p(uint8_t index,char *data)
{
	sprintf(system_data.radio_data.radio_p[index],"%s",data);
}
char *system_get_radio_p(uint8_t index)
{
	return system_data.radio_data.radio_p[index];
}


int system_save_config(void)
{

	const char *config_fime_path = "/spiffs/system/config.json";
	uint32_t file_size=0;

	cJSON* root = NULL;

	root = cJSON_CreateObject();

	//----system set ----//
	cJSON* arr_system_set = cJSON_CreateArray();
	cJSON* obj_system_set = cJSON_CreateObject();

	cJSON_AddNumberToObject(obj_system_set, "wifi switch",system_get_wifi_kaiguan());
	cJSON_AddNumberToObject(obj_system_set , "backlight", system_data.backlight);
	cJSON_AddNumberToObject(obj_system_set , "language", system_get_Language());
	cJSON_AddStringToObject(obj_system_set , "bilibili id", system_data.bilibili_Data.id);

	cJSON_AddItemToArray(arr_system_set, obj_system_set);
	cJSON_AddItemToObject(root, "system set", arr_system_set);
	//----system set ----//


	//----radio data ----//
	cJSON* arr_radio_data = cJSON_CreateArray();
	cJSON* obj_radio_data = cJSON_CreateObject();
	cJSON_AddNumberToObject(obj_radio_data, "background",system_data.radio_data.background);
	cJSON_AddStringToObject(obj_radio_data , "p1", system_data.radio_data.radio_p[0]);
	cJSON_AddStringToObject(obj_radio_data , "p2", system_data.radio_data.radio_p[1]);
	cJSON_AddStringToObject(obj_radio_data , "p3", system_data.radio_data.radio_p[2]);
	cJSON_AddStringToObject(obj_radio_data , "p4", system_data.radio_data.radio_p[3]);
	cJSON_AddStringToObject(obj_radio_data , "p5", system_data.radio_data.radio_p[4]);


	cJSON_AddItemToArray(arr_radio_data, obj_radio_data);
	cJSON_AddItemToObject(root, "radio data", arr_radio_data);
	//----radio data ----//

	//----clock data ----//
	cJSON* arr_clock_data = cJSON_CreateArray();
	cJSON* obj_clock_data = cJSON_CreateObject();
	cJSON_AddNumberToObject(obj_clock_data, "type",system_data.clock_data.type);
	cJSON_AddNumberToObject(obj_clock_data , "style", system_data.clock_data.style);

	cJSON_AddItemToArray(arr_clock_data, obj_clock_data);
	cJSON_AddItemToObject(root, "clock data", arr_clock_data);
	//----clock data ----//

	//----wifi ----//
	cJSON* arr_wifi= cJSON_CreateArray();
	cJSON* obj_wifi = cJSON_CreateObject();
	cJSON_AddStringToObject(obj_wifi, "name",system_data.wifi_name);
	cJSON_AddStringToObject(obj_wifi, "password",system_data.wifi_password);
	cJSON_AddStringToObject(obj_wifi, "auto connect","true");

	cJSON_AddItemToArray(arr_wifi, obj_wifi);
	cJSON_AddItemToObject(root, "wifi", arr_wifi);
	//----wifi ----//

	char* str = cJSON_PrintUnformatted(root);
	int len = strlen(str);
	system_debug("len:%d %s\n",len, cJSON_Print(root));
	system_debug("保存文件");
	file_size = system_get_file_size(config_fime_path);
    FILE* file = fopen(config_fime_path, "w+");
    if (file == NULL)
	{
        system_debug("打开文件失败");
        return 1;
    }
	system_debug("打开文件成功");
	fwrite(str, 1, len, file);
    fclose(file);

	system_debug("保存配置文件结束");
	cJSON_Delete(root);
	return 0;
}

int system_set_Language(int dat)
{
	int Language = system_data.Language;
	system_data.Language = dat;
	system_data.uart_rx_flag=0;

	return Language;
}

int system_get_Language(void)
{
	return system_data.Language;
}

int system_set_wifi_kaiguan(int dat)
{
	int dat1 = system_data.wifi_kaiguan;
	system_data.wifi_kaiguan = dat;
	return dat1;
}

int system_get_wifi_kaiguan(void)
{
	return system_data.wifi_kaiguan;
}



uint32_t system_get_file_size(char * path)
{
	uint32_t fileSize=0;
	FILE *pread = fopen(path,"rb");
	if (pread==NULL)
	{
		system_debug("文件打开失败");
		return;
	}

	fseek(pread, 0, SEEK_END);
	fileSize = ftell(pread);
	fseek(pread,0,SEEK_SET);
	fclose(pread);
	system_debug("文件:%s 大小:%d",path,fileSize);
	return fileSize;
}
void system_set_spk_switch(uint8_t data)
{
	system_data.spk_switch = data;
#if USE_WIN == 0
	bsp_spk_sw_set(system_data.spk_switch);
#endif

	system_debug("set spk switch:%d",data);
}

void system_audio_select(uint8_t data)
{
	system_data.audio_select = data;
#if USE_WIN == 0
	bsp_audio_select(system_data.spk_switch);
#endif
	if(data == 0)
	{

		system_debug("audio selece radio");
	}else
	if(data == 1)
	{

		system_debug("audio selece i2s");
	}
}


int system_get_radio_vol(void)
{
	return system_data.radio_data.vol;
}

void system_set_radio_vol(int data)
{
	system_data.radio_data.vol = data;
	if(system_data.radio_data.vol == 0)
	{
#if USE_WIN == 0
		rda5807_mute_set(1);
		system_set_spk_switch(SPK_SWITCH_OFF);
#endif
	}else
	{
#if USE_WIN == 0
		system_set_spk_switch(SPK_SWITCH_ON);
		rda5807_mute_set(0);
        rda5807_vol_set(system_data.radio_data.vol);
#endif

	}
	system_debug("set vol:%d",data);
}
void system_set_radio_mute(int data)
{
	system_data.radio_data.mute = data;
	if(system_data.radio_data.mute == 0)
	{
#if USE_WIN == 0
		rda5807_mute_set(0);
#endif

		system_debug("mute close");
	}else
	{
#if USE_WIN == 0
		rda5807_mute_set(1);
#endif
		system_debug("mute open");
	}
}

int system_get_radio_sig(void)
{
	return system_data.radio_data.set_sig;
}

void system_set_radio_sig(int data)
{
	system_data.radio_data.set_sig = data;
#if USE_WIN == 0
		rda5807_seekth_set(system_data.radio_data.set_sig);
#endif
	system_debug("set sig:%d",data);
}
int system_set_radio_next_search(void)
{
	system_debug("next search");
	int temp  = 0;
	system_data.radio_data.serrch_status = false;
#if USE_WIN == 0
		//rda5807_seek_direction(1);
		//rda5807_task_start_search();
		rda5807_task_seek(1);
#else
	temp = rand()%2100+8700;
	system_set_radio_frequency(temp);
#endif

	return temp;

}
int system_set_radio_last_search(void)
{
	system_debug("last search");
	int temp  = 0;
	system_data.radio_data.serrch_status = false;
#if USE_WIN == 0
		//rda5807_seek_direction(0);
		//rda5807_task_start_search();
		rda5807_task_seek(0);
#else
	temp = rand()%2100+8700;
	system_set_radio_frequency(temp);
#endif
	return temp;
}
void system_set_radio_search_status(uint8_t data)
{

	system_data.radio_data.serrch_status = data;
}
uint8_t system_get_radio_search_status(void)
{

	return system_data.radio_data.serrch_status;
}


int system_get_radio_frequency(void)
{
#if USE_WIN == 0
	system_data.radio_data.frequency = rda5807_freg_get()*10;
#endif
	system_debug("get frequency:%d",system_data.radio_data.frequency);
	return system_data.radio_data.frequency;
}

void system_set_radio_frequency(int data)
{

	system_data.radio_data.frequency = data;
#if USE_WIN == 0
	//rda5807_freq_set(system_data.radio_data.frequency);  //频率设置
	rda5807_task_set_frequency(system_data.radio_data.frequency);
#endif

	system_debug("set frequency:%d",data);

}

void system_set_radio_onoff(int data)
{
	system_data.radio_data.onoff = data;
	if(system_data.radio_data.onoff == 0)
	{
		system_debug("close radio");
#if USE_WIN == 0
	rda5807_task_set_off();
#endif
	}else
	if(system_data.radio_data.onoff == 1)
	{
		system_debug("open radio");
#if USE_WIN == 0
	rda5807_task_set_on();
#endif

	}
}

int system_get_radio_onoff(void)
{
	return system_data.radio_data.onoff;
}

void system_power_off(void)
{
	system_debug("power off");
#if USE_WIN == 0

	bsp_power_send_off();
#endif

}
uint8_t system_get_power_on_type(void)
{
	system_debug("get power on type");
#if USE_WIN == 0
	return bsp_power_get_on_type();
#endif
	return BSP_POWER_ON_TYPE_USB;
}
void system_set_clock_style(int data)
{
	system_data.clock_data.style = data;
}
int system_get_clock_style(void)
{
	return system_data.clock_data.style;
}
